package com.qtenv.datasync.worker.tuoanxin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.qtenv.datasync.api.tuoanxin.TuoAnXinApi;
import com.qtenv.datasync.base.ArchiveSyncer;
import com.qtenv.datasync.constant.AppConstants;
import com.qtenv.datasync.service.entity.Terminals_bf;
import com.qtenv.datasync.service.entity.tuoanxin.TAXTerminals;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName TAXArchiveSyncWorker
 * @Author CBL
 * @Date 2022/2/28 15:35
 **/
@Component
public class TAXArchiveSyncWorker extends ArchiveSyncer {
    public TAXArchiveSyncWorker() {
        super("拓安信");
    }

    @Override
    public List<Terminals_bf> getTerminals() {
        String terminalData = TuoAnXinApi.GetDeviceInfo();
        List<Terminals_bf> terminals = this.transform(terminalData);
        terminals.removeIf(item->item.getTerminalName().indexOf("博皇家居")==-1);

        System.out.println("从厂家【" + this.getSupplier() + "】获取【档案】数量：" + terminals.size());
        return terminals;
    }

    private List<Terminals_bf> transform(String terminalData) {
        List<Terminals_bf> result = CollectionUtil.newArrayList();

        List<TAXTerminals> taxTerminalList = JSON.parseArray(JSON.parseObject(terminalData).getJSONObject("body").getString("values"), TAXTerminals.class);
        taxTerminalList.forEach(taxTerminals -> {
            Terminals_bf terminals = new Terminals_bf();
            BeanUtil.copyProperties(taxTerminals, terminals, "Caliber");
            terminals.setTenant_id(AppConstants.APP_TENANT_ID);
            terminals.setSupplierID(AppConstants.TAX_SUPPLIER_ID);
            terminals.setTerminalMark(1);
            terminals.setUserArea(AppConstants.APP_TENANT_ID);
            String caliber = taxTerminals.getCaliber();
            if (StrUtil.isNotEmpty(caliber)) {
                caliber = StrUtil.replaceIgnoreCase(caliber, "DN", "");
                caliber = StrUtil.replaceIgnoreCase(caliber, "mm", "");
                terminals.setCaliber(Integer.valueOf(caliber));
            }

            result.add(terminals);
        });

        return result;
    }

    public static void main(String[] args) {
        TAXArchiveSyncWorker worker = new TAXArchiveSyncWorker();
        System.out.println(JSONUtil.toJsonPrettyStr(worker.getTerminals()));
//        worker.syncTerminals();
    }
}
